package com.delamer.facile.exe;

import com.delamer.facile.db.DBUtilPermit;
import com.delamer.facile.obj.ObjectPermit;
import com.delamer.utils.MsgUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ExecutorPermit {
    public void help(CommandSender sender, String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Bukkit.getPluginManager().getPlugin("DelamerFacile").getResource("permit.help"), "UTF-8"));
        List<String> results = new ArrayList<String>();
        String line = null;
        while ((line = bufferedReader.readLine()) != null)
            results.add(line);
        sender.sendMessage(results.toArray(new String[]{}));
    }

    public void list(CommandSender sender, String[] args) throws Exception {
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        List<String> list = Bukkit.getPluginManager().getPlugin("DelamerFacile").getConfig().getStringList("permit");
        String msg = "§e需授权的指令列表：§f";
        for (String str : list) msg = msg + str + ", ";
        if (msg.endsWith(", ")) msg = msg.substring(0, msg.length() - 2);
        else msg = msg + "无";
        sender.sendMessage(msg);
    }

    public void info(CommandSender sender, String[] args) throws Exception {
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length == 1) {
            sender.sendMessage(MsgUtil.err("目标玩家不能为空"));
            return;
        }
        Player target = Bukkit.getPlayerExact(args[1]);
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        String msg = "§e玩家§b" + args[1] + "§e已授权的指令列表：§f";
        List<ObjectPermit> list = DBUtilPermit.getPermitList(args[1]);
        for (ObjectPermit obj : list)
            msg = msg + obj.getPermitName() + ", ";
        if (msg.endsWith(", ")) msg = msg.substring(0, msg.length() - 2);
        else msg = msg + "无";
        sender.sendMessage(msg);
    }

    public void add(CommandSender sender, String[] args) throws Exception {
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length < 3) {
            sender.sendMessage(MsgUtil.err("参数数量不足，目标玩家不能为空，目标指令组不能为空"));
            return;
        }
        Player target = Bukkit.getPlayerExact(args[1]);
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        if (DBUtilPermit.getPermit(args[1], args[2]) != null) {
            sender.sendMessage(MsgUtil.err("添加权限失败，目标玩家已拥有对应权限组"));
            return;
        }
        ObjectPermit object = new ObjectPermit() {{
            setUserName(args[1]);
            setPermitName(args[2]);
        }};
        DBUtilPermit.addPermit(object);
        sender.sendMessage(MsgUtil.msg("添加权限成功：§b" + args[1] + " §7-> §a" + args[2]));
    }

    public void del(CommandSender sender, String[] args) throws Exception {
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length < 3) {
            sender.sendMessage(MsgUtil.err("参数数量不足，目标玩家不能为空，目标指令组不能为空"));
            return;
        }
        Player target = Bukkit.getPlayerExact(args[1]);
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        if (DBUtilPermit.getPermit(args[1], args[2]) == null) {
            sender.sendMessage(MsgUtil.err("删除权限失败，目标玩家未拥有对应权限组"));
            return;
        }
        ObjectPermit object = new ObjectPermit() {{
            setUserName(args[1]);
            setPermitName(args[2]);
        }};
        DBUtilPermit.delPermit(object);
        sender.sendMessage(MsgUtil.msg("删除权限成功：§b" + args[1] + " §7-> §m§c" + args[2]));
    }
}
